@extends('admin.base')
@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <div class="layui-btn-group">
                <button class="layui-btn layui-btn-sm" id="create">添 加</button>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="mode">
                @{{# if(d.mode == 1){ }}
                否
                @{{# }else{ }}
                是
                @{{# } }}
            </script>
            <script type="text/html" id="pictureTpl">
                <a href ="javascript:void(0)"><img src="@{{d.picture}}" alt="" style="width: 100%;height: 100%;"></a>
            </script>
            <script type="text/html" id="switchTpl">
                @{{# if(d.state == 1){ }}
                <input type="checkbox" name="switch" value="@{{d.id}}" lay-filter="state" checked lay-skin="switch" lay-text="启用|禁止">
                @{{# }else{ }}
                <input type="checkbox" value="@{{d.id}}" name="switch" lay-filter="state" lay-skin="switch" lay-text="启用|禁止">
                @{{# } }}
            </script>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                    <!--<a class="layui-btn layui-btn-sm" lay-event="password">修改密码</a>
                    <a class="layui-btn layui-btn-danger layui-btn-sm " lay-event="del">删除</a>-->
                </div>
            </script>
        </div>
    </div>
@endsection
@section('script')
    <script type="text/javascript">
        layui.use(['layer','table','form','laytpl'],function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            var laytpl = layui.laytpl;

            //用户表格初始化
            var dataTable = table.render({
                elem: '#dataTable'
                ,height: 500
                ,url: "/admin/api/types" //数据接口
                ,page: false //开启分页
                ,id:'d'
                ,cols: [[ //表头
                    {checkbox: true,fixed: true}
                    ,{field: 'id', title: 'ID', sort: true,width:80}
                    ,{field: 'name', title: '栏目名称'}
                    ,{field: 'picture', width: 120, title: 'banner图',templet: '#pictureTpl',event:'picture'}
                    ,{field: 'sort',width: 120, title: '排序'}
                    ,{field: 'state',width:180, title: '状态',templet: '#switchTpl'}
                    ,{field: 'updated_at',width: 220, title: '更新时间'}
                    ,{fixed: 'right', width: 180,title:'操作', align:'center', toolbar: '#options'}
                ]]
            });

            //监听工具条
            table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    ,layEvent = obj.event; //获得 lay-event 对应的值
                if(layEvent === 'del'){
                    layer.confirm('确认删除吗？', function(index){
                        $.post("",{_method:'delete',ids:[data.id]},function (result) {
                            if (result.code === 0){
                                obj.del(); //删除对应行（tr）的DOM结构
                            }
                            layer.close(index);
                            layer.msg(result.message,{icon:6})
                        });
                    });
                } else if(layEvent === 'edit'){
                    location.href = '/admin/view/type/'+data.id+'/edit';
                } else if(layEvent === 'picture') {
                    if (data.picture !== '') {
                        let html = '<img src="'+data.picture+'" alt="" style="width: 100%;height: 100%;">';
                        layer.open({
                            title:'',
                            type:1,
                            ShadeClose:true,
                            content:html
                        });
                    }
                }
            });

            form.on('switch(state)', function(data){
                if(data.elem.checked === true){
                    val = 1;
                }else{
                    val = 0;
                }
                $.get("/admin/type/state",{ids:[data.value],state:val},function (result) {
                    if (result.code === 0){
                        layer.msg(result.message,{icon:1});
                    }else{
                        layer.msg(result.message,{icon:6});
                    }
                });
            });

            $('#create').click(function () {
                location.href = '/admin/view/type/store';
            });

            //按钮批量禁用
            $("#listDelete").click(function () {

                var ids = [];
                var hasCheck = table.checkStatus('d');
                var hasCheckData = hasCheck.data;

                if (hasCheckData.length>0){
                    $.each(hasCheckData,function (index,element) {
                        ids.push(element.id)
                    });
                }

                if (ids.length>0){
                    layer.confirm('确认禁用？', function(index){
                        $.get("/admin/type/state",{ids:ids,state:0},function (result) {
                            if (result.code === 0){
                                dataTable.reload();
                            }
                            layer.close(index);
                            layer.msg(result.message,{icon:6})
                        });
                    });
                }else {
                    layer.msg('请选择操作项',{icon:5})
                }
            })
        })
    </script>
@endsection



